<template>
  <div class="app-container">
    <el-row :gutter="20">
      <!--部门数据-->
      <el-col :span="4" :xs="24">
        <div class="head-container">
          <el-input
            v-model="queryParams.teamId"
            placeholder="请输入部门名称"
            clearable
            size="small"
            prefix-icon="el-icon-search"
            style="margin-bottom: 20px"
          />
        </div>
        <div class="head-container">
          <el-tree
            :data="deptOptions"
            :props="defaultProps"
            :expand-on-click-node="false"
            :filter-node-method="filterNode"
            ref="tree"
            node-key="value"
            default-expand-all
            highlight-current
            @node-click="handleNodeClick"
          />
        </div>
      </el-col>
      <el-col :span="20" :xs="24">
    <el-form :model="queryParams" ref="queryForm" size="small" :inline="true" v-show="showSearch" label-width="120px">
      <el-form-item label="账号" prop="account">
        <el-input
          v-model="queryParams.account"
          placeholder="请输入账号"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="电话号码" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入电话号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="真实姓名" prop="realName">
        <el-input
          v-model="queryParams.realName"
          placeholder="请输入真实姓名"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="角色" prop="role">
        <el-select v-model="queryParams.role" placeholder="请选择">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value">
          </el-option>
        </el-select>
      </el-form-item>
<!--      <el-form-item label="所属志愿者队伍" prop="teamId">-->
<!--        <el-input-->
<!--          v-model="queryParams.teamId"-->
<!--          placeholder="请输入所属志愿者队伍"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="政治身份" prop="politicalIdentityId">-->
<!--        <el-input-->
<!--          v-model="queryParams.politicalIdentityId"-->
<!--          placeholder="请输入政治身份，0:群众 1:中共党员 2:中共预备党员 3:共青团员 4:无党派民主人士"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->

      <el-form-item label="联系地址" prop="contactAddress">
        <el-input
          v-model="queryParams.contactAddress"
          placeholder="请输入联系地址"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="积分" prop="point">
        <el-input
          v-model="queryParams.point"
          placeholder="请输入积分"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="工时" prop="workHours">-->
<!--        <el-input-->
<!--          v-model="queryParams.workHours"-->
<!--          placeholder="请输入工时"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
<!--      <el-form-item label="微信的openid，绑定微信时写入" prop="openId">-->
<!--        <el-input-->
<!--          v-model="queryParams.openId"-->
<!--          placeholder="请输入微信的openid，绑定微信时写入"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item label="身份证号码" prop="idNumber">
        <el-input
          v-model="queryParams.idNumber"
          placeholder="请输入身份证号码"
          clearable
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
<!--      <el-form-item label="特长，掌握的(生活)技能" prop="forte">-->
<!--        <el-input-->
<!--          v-model="queryParams.forte"-->
<!--          placeholder="请输入特长，掌握的(生活)技能"-->
<!--          clearable-->
<!--          @keyup.enter.native="handleQuery"-->
<!--        />-->
<!--      </el-form-item>-->
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['api:user:add']"
        >新增</el-button>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="success"-->
<!--          plain-->
<!--          icon="el-icon-edit"-->
<!--          size="mini"-->
<!--          :disabled="single"-->
<!--          @click="handleUpdate"-->
<!--          v-hasPermi="['api:user:edit']"-->
<!--        >修改</el-button>-->
<!--      </el-col>-->
      <el-col :span="1.5">
        <el-button
          type="danger"
          plain
          icon="el-icon-delete"
          size="mini"
          :disabled="multiple"
          @click="handleDelete"
          v-hasPermi="['api:user:remove']"
        >删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-upload
          action
          class="upload-demo"
          :http-request="selectPicListUpload"
          :file-list="fileList"
          :on-remove="handleRemove"
        >
          <el-button icon="el-icon-plus" size="mini" plain type="primary">批量导入</el-button>
        </el-upload>
      </el-col>
<!--      <el-col :span="1.5">-->
<!--        <el-button-->
<!--          type="warning"-->
<!--          plain-->
<!--          icon="el-icon-download"-->
<!--          size="mini"-->
<!--          @click="handleExport"-->
<!--          v-hasPermi="['api:user:export']"-->
<!--        >导出</el-button>-->
<!--      </el-col>-->
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="userList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
<!--      <el-table-column label="主键" align="center" prop="pkId" />-->
      <el-table-column width="120" label="电话号码" align="center" prop="phone"/>

      <el-table-column width="100" label="真实姓名" align="center" prop="realName" />
      <el-table-column width="120" label="昵称" align="center" prop="nickName">
        <template slot-scope="scope">
          <span>{{scope.row.nickName.slice(0,7)}}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="所属慈善机构，管理员分配" align="center" prop="organizationId" />-->
      <el-table-column label="角色" align="center" prop="role">
        <template slot-scope="scope">
          <span v-if="scope.row.role===0">普通用户</span>
          <span v-if="scope.row.role===1">志愿者</span>
        </template>
      </el-table-column>

      <el-table-column width="180" label="账号注册时间" align="center" prop="registerDatetime">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.registerDatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="密码" align="center" prop="password" />-->
      <el-table-column label="加入志愿者时间" align="center" prop="joinDatetime" width="120">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.joinDatetime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="所属志愿者队伍" align="center" prop="teamId" />-->
      <el-table-column width="180" label="工作单位" align="center" prop="company" />
      <el-table-column label="政治面貌" align="center" prop="politicalIdentityId">
        <template slot-scope="scope">
          <span v-if="scope.row.politicalIdentityId===0">群众</span>
          <span v-if="scope.row.politicalIdentityId===1">中共党员</span>
          <span v-if="scope.row.politicalIdentityId===2">中共预备党员</span>
          <span v-if="scope.row.politicalIdentityId===3">共青团员</span>
          <span v-if="scope.row.politicalIdentityId===4">无党派民主人士</span>
        </template>
      </el-table-column>
<!--      <el-table-column label="加入志愿说明" align="center" prop="joinDescription" />-->
      <el-table-column width="180" label="账号" align="center" prop="account" />
<!--      <el-table-column label="0: 未实名认证，1：已实名认证" align="center" prop="realNameAuthenticated" />-->
<!--      <el-table-column label="页面分区" align="center" prop="systemArea" />-->
      <el-table-column  width="180"label="联系地址" align="center" prop="contactAddress" />
<!--      <el-table-column label="头像url" align="center" prop="avatar" />-->
      <el-table-column label="积分" align="center" prop="point" />
      <el-table-column label="工时" align="center" prop="workHours" />
<!--      <el-table-column label="微信的openid，绑定微信时写入" align="center" prop="openId" />-->
<!--      <el-table-column label="身份证号码" align="center" prop="idNumber" />-->
      <el-table-column width="180" label="生日" align="center" prop="birthDate">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.birthDate, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="性别" align="center" prop="sex">
        <template slot-scope="scope">
          <el-tag v-if="scope.row.sex===0">男</el-tag>
          <el-tag v-if="scope.row.sex===1">女</el-tag>
          <el-tag v-if="scope.row.sex===null">未知</el-tag>
        </template>
      </el-table-column>
<!--      <el-table-column label="特长，掌握的(生活)技能" align="center" prop="forte" />-->
      <el-table-column width="180" label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['api:user:edit']"
          >修改</el-button>
          <el-button
            size="mini"
            type="text"
            icon="el-icon-delete"
            @click="handleDelete(scope.row)"
            v-hasPermi="['api:user:remove']"
          >删除</el-button>
        </template>
      </el-table-column>
    </el-table>
    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="column.pageNum"
      :limit.sync="column.pageSize"
      @pagination="getList"
    />
      </el-col>
    </el-row>
    <!-- 添加或修改用户列表对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="800px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="120px">
        <el-form-item label="电话号码" prop="phone">
          <el-input v-model="form.phone" placeholder="请输入电话号码" />
        </el-form-item>
        <el-form-item label="真实姓名" prop="realName">
          <el-input v-model="form.realName" placeholder="请输入真实姓名" />
        </el-form-item>
        <el-form-item label="昵称" prop="nickName">
          <el-input v-model="form.nickName" placeholder="请输入昵称" />
        </el-form-item>
        <el-form-item label="性别" prop="sex">
          <el-select v-model="form.sex" placeholder="请选择">
            <el-option
              v-for="item in sex"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="角色" prop="role">
          <el-select v-model="form.role" placeholder="请选择">
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item v-if="form.role==1" label="所属志愿者队伍" prop="teamId">
<!--          <el-input v-model="form.teamIdList" placeholder="请输入所属志愿者队伍" />-->
          <el-select v-model="form.teamIdList" multiple placeholder="请选择">
            <el-option
              v-for="item in duiwoptions"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="工作单位" prop="company">
          <el-input v-model="form.company" placeholder="请输入工作单位，用户自己填" />
        </el-form-item>
        <el-form-item label="政治面貌" prop="politicalIdentityId">
          <el-select v-model="form.politicalIdentityId" placeholder="请选择">
            <el-option
              v-for="item in politicalIdentityId"
              :key="item.value"
              :label="item.label"
              :value="item.value">
            </el-option>
          </el-select>
<!--          <el-input v-model="form.politicalIdentityId" placeholder="请输入政治身份，0:群众 1:中共党员 2:中共预备党员 3:共青团员 4:无党派民主人士" />-->
        </el-form-item>
        <el-form-item label="加入志愿说明" prop="joinDescription">
          <el-input v-model="form.joinDescription" type="textarea" placeholder="请输入内容" />
        </el-form-item>
        <el-form-item label="联系地址" prop="contactAddress">
          <el-input v-model="form.contactAddress" placeholder="请输入联系地址" />
        </el-form-item>
<!--        <el-form-item label="头像url" prop="avatar">-->
<!--          <el-input v-model="form.avatar" placeholder="请输入头像url" />-->
<!--        </el-form-item>-->
        <el-form-item label="积分" prop="point">
          <el-input v-model="form.point" placeholder="请输入积分" />
        </el-form-item>
        <el-form-item label="工时" prop="workHours">
          <el-input v-model="form.workHours" placeholder="请输入工时" />
        </el-form-item>
        <el-form-item label="身份证号码" prop="idNumber">
          <el-input v-model="form.idNumber" placeholder="请输入身份证号码" />
        </el-form-item>
        <el-form-item label="生日" prop="birthDate">
          <el-date-picker clearable
            v-model="form.birthDate"
            type="date"
            value-format="yyyy-MM-dd"
            placeholder="请选择生日">
          </el-date-picker>
        </el-form-item>
        <el-form-item label="特长" prop="forte">
          <el-input v-model="form.forte" placeholder="请输入特长，掌握的(生活)技能" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {listUser, getUser, delUser, addUser, updateUser, importUser, teamlist} from "@/api/user/user";

export default {
  name: "User",
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 用户列表表格数据
      userList: [],
      // 弹出层标题
      title: "",
      duiwoptions:[],
      // 是否显示弹出层
      open: false,
      politicalIdentityId:[{
        value: 0,
        label: '群众'
      }, {
        value: 1,
        label: '中共党员'
      },{
        value: 2,
        label: '中共预备党员'
      }, {
        value: 3,
        label: '共青团员'
      }, {
        value: 4,
        label: '无党派民主人士'
      }],
      options: [{
        value: 0,
        label: '普通用户'
      }, {
        value: 1,
        label: '志愿者'
      }],
      deptOptions:[
        {
          value:-1,
          label: '普通用户'
        },{
        value: 1,
        label: '志愿者',
        children: []
      }],
      defaultProps: {
        children: 'children',
        label: 'label'
      },
      sex: [{
        value: 0,
        label: '男'
      }, {
        value: 1,
        label: '女'
      }],
      // 查询参数
      queryParams: {
        pkId: null,
        phone: null,
        realName: null,
        nickName: null,
        role: 0,
        teamId: null,
        company: null,
        politicalIdentityId: null,
        joinDescription: null,
        contactAddress: null,
        avatar: null,
        point: null,
        workHours: null,
        idNumber: null,
        birthDate: null,
        sex: null,
        forte: null
      },
      //分页
      column:{
        pageSize:null,
        pageNum:null
      },
      // 表单参数
      form: {
        pkId: null,
        phone: null,
        realName: null,
        nickName: null,
        role: null,
        teamIdList: [],
        company: null,
        politicalIdentityId: null,
        joinDescription: null,
        contactAddress: null,
        avatar: null,
        point: null,
        workHours: null,
        idNumber: null,
        birthDate: null,
        sex: null,
        forte: null
      },
      // 表单校验
      rules: {
        phone: [
          { required: true, message: '请输入手机号', trigger: 'change'}
        ],
        nickName: [
          { required: true, message: '请输入姓名', trigger: 'change'}
        ],
        role: [
          { required: true, message: '请选择角色', trigger: 'change'}
        ],
        teamIdList: [
          { required: true, message: '必须选择自愿队伍', trigger: 'change'}
        ],
        politicalIdentityId: [
          { required: true, message: '必须选择政治身份', trigger: 'change'}
        ],
        workHours: [
          { required: true, message: '请输入工时', trigger: 'change'}
        ],
        point: [
          { required: true, message: '请输入积分', trigger: 'change'}
        ]
      },
      fileList: []
    };
  },
  created() {
    this.getList();
  },
  methods: {
    /** 查询用户列表列表 */
    getList() {
      this.loading = true;
      listUser(this.queryParams,this.column).then(response => {
        this.userList =response.pageData;
        this.total = response.total;
        this.loading = false;
      });
      teamlist().then(res =>{
        this.duiwoptions = res.data
        this.duiwoptions = this.duiwoptions.map(item =>({
          label:item.name,
          value:item.id
        }))
        this.deptOptions[1].children =  this.duiwoptions
      })
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        pkId: null,
        phone: null,
        realName: null,
        nickName: null,
        role: null,
        teamIdList: [],
        company: null,
        politicalIdentityId: null,
        joinDescription: null,
        contactAddress: null,
        avatar: null,
        point: null,
        workHours: null,
        idNumber: null,
        birthDate: null,
        sex: null,
        forte: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.pkId)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加用户列表";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const pkId = row.pkId || this.ids
      getUser(pkId).then(response => {
        console.log(response.data);
        this.form = {
          pkId: response.data.pkId,
          phone: response.data.phone,
          realName: response.data.realName,
          nickName: response.data.nickName,
          role: response.data.role,
          teamIdList: [],
          company: response.data.company,
          politicalIdentityId: response.data.politicalIdentityId,
          joinDescription: response.data.joinDescription,
          contactAddress: response.data.contactAddress,
          avatar: response.data.avatar,
          point: response.data.point,
          workHours: response.data.workHours,
          idNumber: response.data.idNumber,
          birthDate: response.data.birthDate,
          sex: response.data.sex,
          forte: response.data.forte
        }
        console.log(response.data.teamIdNamePairList)
        this.form.teamIdList = response.data.teamIdNamePairList.map(i =>(i.id))
        this.open = true;
        this.title = "修改用户列表";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.pkId != null) {
            updateUser(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addUser(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const pkIds = row.pkId || this.ids;
      this.$modal.confirm('是否确认删除用户列表编号为"' + pkIds + '"的数据项？').then(function() {
        return delUser(pkIds);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      this.download('api/user/export', {
        ...this.queryParams
      }, `user_${new Date().getTime()}.xlsx`)
    },
    handleRemove(file, fileList) {
      console.log(file, fileList);
    },
    handlePreview(res) {
      console.log(res);
    },
    handleExceed(files, fileList) {
      this.$message.warning(`当前限制选择 3 个文件，本次选择了 ${files.length} 个文件，共选择了 ${files.length + fileList.length} 个文件`);
    },
    //商品介绍图片
    selectPicListUpload(obj) {
      let fd = new FormData(); //参数的格式是formData格式的
      fd.append("multipartFile", obj.file); //参数
      importUser(fd).then(res => {
        console.log(res)
        this.fileList.push({
          name: 'excel', url: this.imgUrl + res.msg
        })
      });
    },
    // 树形结构
    handleNodeClick(data) {
      if (data.value === -1){
        this.queryParams.teamId = null
        this.queryParams.role = 0
        this.getList()
      }else {
        console.log(data.value);
        this.queryParams.teamId = data.value
        this.queryParams.role = 1
        this.getList()
      }
    },
    filterNode(value, data) {
      if (!value) return true;
      return data.label.indexOf(value) !== -1;
    }
  }
};
</script>
